<?php

/**
 * Tabela do texto inicial.
 *
 * @author Fernando Dias Oliveira <rodox17@gmail.com>
 * @package lore
 * @subpackage model
 */

class caf02 extends Zend_Db_Table_Abstract  
{
	protected $_table   = "caf02";
	protected $_primary = "caf02_cod";
	
	public function getUserByPass($pass)
	{
	}
	
	public function getDesconto($cod_pedido)
	{
		$obj = $this->find($cod_pedido)->current();
		
		return $obj;
	}

	public function saveDesconto($post)
	{
		$this->update(array("caf02_desconto" => $post["desconto"]), "caf02_cod = {$post["caf04_cod_item"]}");
	}
	
	public function getAllById($id)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf01, caf02 WHERE caf02_cod_user = caf01_cod AND caf02_cod = '$id'");
	}
	
	public function getLastByMesa($id)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf01, caf02 WHERE caf02_cod_user = caf01_cod AND caf02_mesa = '$id' ORDER BY caf02_cod DESC");
	}
	
	public function getAllByGroup($id)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf01, caf02 WHERE caf02_cod_user = caf01_cod AND caf02_cod = '$id'");
	}
	
	public function getMesa($mesa)
	{
		return $this->fetchAll("caf02_mesa = $mesa AND caf02_active = 1")->toArray();
	}
	
	public function getAll()
	{
		return $this->fetchAll("caf02_active = 1")->toArray();
	}
	
	public function save($arrPost)
	{
		//print_r($arrPost);
		//die;
		
		
		//die($cod);
		
		if($cod = $arrPost["caf02_cod"])
		{
			$this->update($arrPost, "caf02_cod = $cod");
			
			return;
		}
		else 
		{
			unset($arrPost["caf02_cod"]);
		}
		
		return $this->insert($arrPost);
	}
	
	public function getPedidoByCod($cod_pedido)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf02, caf03, caf04, caf05 WHERE caf02_cod = '$cod_pedido' AND caf03_cod = caf04_cod_menu AND caf04_cod_menu = caf05_cod");
		
	}
	
	public function getPedidoByCod2($cod_pedido)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf01, caf02, caf03, caf04, caf05 WHERE caf01_cod = caf02_cod_user AND caf04_cod_item = $cod_pedido AND caf02_cod = '$cod_pedido' AND caf03_cod = caf04_cod_menu AND caf04_cod_prod = caf05_cod");
		
	}
	
	public function getPedidoByMesa($cod)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT DISTINCT(caf04_cod_prod) AS prod FROM caf02, caf04 WHERE caf02_cod = $cod AND caf04_cod_item = caf02_cod");
	}
	
	public function disactivePedido($cod_pedido, $cod_usuario, $forma_pagamento)
	{
		$this->update(array("caf02_active" => 0, "caf02_forma_pagamento" => $forma_pagamento, "caf02_usuario_fechou_mesa" => $cod_usuario), "caf02_cod = $cod_pedido");
	}
	
	public function deleteById($id)
	{
		$this->delete("caf02_cod = $id");
	}
	
	public function getProductsPedido()
	{
		$arr = $this->fetchAll("SELECT * FROM caf02, caf03, caf04, caf05 WHERE caf02_cod = caf04_cod_item AND caf03_cod = caf04_cod_menu AND caf04_cod_menu = caf05_cod")->toArray();
		
	}
}

?>
